package com.atguigu.servlet.model;

import com.atguigu.bean.Book;
import com.atguigu.bean.Cart;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.servlet.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CartServlet extends BaseServlet {
    //flag='addCart
    //添加购物车
    protected void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("---------------");
        //1.先进判断是否有购物车
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if(cart==null){
             cart = new Cart();
             session.setAttribute("cart",cart);
        }
        System.out.println("222222222222222222222222222222222");

//        response.getWriter().write(""+cart.getTotalCount());
        //2取值
        String id = request.getParameter("id");
        System.out.println("id = " + id);
        BookService bookService = new BookServiceImpl();
        Book book = bookService.selectBookById(id);
        //添加购物车
        cart.addCart(book);
        //给出响应重定向
        //response.sendRedirect(request.getContextPath()+"/user?flag=pagesIndex");
//        String  totalCount =""+cart.getTotalCount();
//        System.out.println("totalCount = " + totalCount);
        response.getWriter().write(""+cart.getTotalCount());
    }
    //flag='subtractCount'
    protected void subtractCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //取值，获取id
        String id = request.getParameter("id");
        //取购物车
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        //进行数量减一
        cart.subtractCount(Integer.parseInt(id));
        if(cart.getTotalCount()==0){
            request.getSession().removeAttribute("cart");
        }
        this.processTemplate("cart/cart",request,response);
    }
    //flag='addCount'
    protected void addCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1.取值，获取id
        String id = request.getParameter("id");
        //2获取购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //进行数量加一
        cart.addCount(Integer.parseInt(id));

         this.processTemplate("cart/cart",request,response);

    }
    // flag='deleteCart'
    protected void deleteCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //先获取id
        String id = request.getParameter("id");
        //获取购物车
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        cart.deleteCartItem(Integer.parseInt(id));
        this.processTemplate("cart/cart",request,response);

    }
    //cart?flag=clearCart
    protected void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //清空购物车
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        request.getSession().removeAttribute("cart");
        this.processTemplate("cart/cart",request,response);
    }
}
